package com.ld.security.server.admin.modules.gl.service.impl;

import cn.hutool.core.lang.Console;
import com.ld.security.common.core.util.ReflectionUtil;
import com.ld.security.common.core.util.StringUtil;
import com.ld.security.common.mybatis.base.constant.SuperConstant;
import com.ld.security.server.admin.modules.gl.entity.GlEnumEntity;
import com.ld.security.server.admin.modules.gl.entity.GlEnumItemEntity;
import com.ld.security.server.admin.modules.gl.po.EnumDetailsPo;
import com.ld.security.server.admin.modules.gl.po.EnumSavePo;
import com.ld.security.server.admin.modules.gl.service.IGlEnumService;
import com.ld.security.server.admin.modules.gl.vo.EnumDetailsVo;
import com.ld.security.common.mybatis.base.service.ISuperServiceImpl;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.Field;

@Service
@Transactional
public class GlEnumServiceImpl  extends ISuperServiceImpl<GlEnumEntity>
implements
        IGlEnumService {


        @Override
        public EnumDetailsVo getEnumDetails(String id) {
                EnumDetailsVo vo = new EnumDetailsVo();
                vo.setCode(id);
                this.proxyQuery(vo);
                return vo;
        }

        @Override
        public int saveEnum(EnumSavePo po) {

                //新增会导致失败
                //旧数据删除
                try{
                        EnumDetailsVo vo = getEnumDetails(po.getParent().getCode());
                        this.proxyRemove(vo);
                }catch (Exception e){

                }


                po.getChildren().sort((x,y) -> {
                        return x.getSortVal() - y.getSortVal();
                });
                int sortVal = 0;
                    for (GlEnumItemEntity glEnumItemEntity : po.getChildren()) {
                            glEnumItemEntity.setSortVal(sortVal  );
                            sortVal+=2;
                    }
                this.proxySave(po);
                return 1;
        }


        private String getPackName(String className){
                StringBuffer sb =  new StringBuffer();
                int b = 0;
                for (char c : className.toCharArray()) {
                        if(StringUtil.isLowerCaseFirstOne(c)){
                                b++;
                        }
                        if(b == 2){
                                break;
                        }
                        sb.append(c);

                }
                return sb.toString();


        }

        @Override
        public int saveEntityClass(Class clazz){


                int total=0;

                String packName =getPackName(clazz.getSimpleName());
                String typeName = clazz.getSimpleName().replaceAll("Entity","");


                for (Field field :ReflectionUtil.getAllDeclaredFields(clazz)) {
                        EnumSavePo po = new EnumSavePo();

                        Class p =  field.getType();

                        if(!p.isEnum()){
                          continue;
                        }else if(!SuperConstant.SuperEnum.class.isAssignableFrom(p)){
                                //非持久化类型
                                continue;
                        }


                        String label="";
                        ApiModelProperty apiModelProperty =  field.getAnnotation(ApiModelProperty.class);
                        if(apiModelProperty != null){
                                label = apiModelProperty.value();
                        }


                        GlEnumEntity glP =  this.newInstance();
                        glP.setLabel(label);
                        glP.setTypeName(typeName);
                        glP.setFieldName(field.getName());
                        glP.setPackName(packName);

                        glP.setCode(glP.getTypeName()+"_"+glP.getFieldName());
                        po.setParent(glP);

                        int sortVal = 0;

                        //字典值
                        for (Object o : p.getEnumConstants()) {
                                SuperConstant.SuperEnum en= (SuperConstant.SuperEnum)o;
                                GlEnumItemEntity item =  new GlEnumItemEntity();
                                item.setSortVal(sortVal++);
                                item.setLabel(en.getLabel());
                                item.setValue(en.getValue());
                                po.addChildren(item);
                        }
                        total+=this.saveEnum(po);
                }
                return total;
        }

}
